﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Repetition.Domain;
using FluentNHibernate.Mapping;

namespace Repetition.Persistence.FluentNHibernate.Mapping
{
    public class ProblemMap : ClassMap<Problem>
    {
        public ProblemMap()
        {
            Id(x => x.Id)
                .GeneratedBy.Native();

            Map(x => x.Answer);
            Map(x => x.Question);

            // Explicitly specify type and sql type to force  Fluent-NHibernate
            // to map the enum to an int. Otherwise it uses GenericEnumMapper
            // (or something like that) and maps it to a string.
            Map(x => x.Difficulty)
                .CustomTypeIs(typeof(Problem.DifficultyLevel))
                .CustomSqlTypeIs("int");
            Map(x => x.Importance)
                .CustomTypeIs(typeof(Problem.ImportanceLevel))
                .CustomSqlTypeIs("int");

            HasManyToMany<Subject>(x => x.Subjects)
                .WithTableName("Problem_Subject")
                .WithParentKeyColumn("ProblemId")
                .WithChildKeyColumn("SubjectId")
                .Cascade.SaveUpdate()
                .Access.AsCamelCaseField()
                .AsBag();
        }
    }
}
